package com.itheima.web.controller.company;

import com.alibaba.druid.util.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.company.Company;
import com.itheima.service.company.CompanyService;
import com.itheima.web.controller.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.UUID;

@Controller
@RequestMapping(value = "/company")
public class CompanyController extends BaseController {

    @Reference
    private CompanyService companyService;


    @RequestMapping(value = "/list", name = "企业列表分页查询")
    @RequiresPermissions("企业管理")
    public String list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "5") Integer size){
        //1、从companyService里调用findAll方法，返回企业列表数据
        //List<Company> list = companyService.findAll();
        PageInfo<Company> pageInfo = companyService.findByPage(page,size);

        //2、将列表数据放入到request域当中
        request.setAttribute("page", pageInfo);

        //3、返回一个非常好看的页面
        //WEB-INF/pages/company/company-list.jsp
        return "company/company-list";
    }

    @RequestMapping(value = "toAdd", name = "跳转到企业添加")
    public String toAdd(){
        return "company/company-add";
    }

    @RequestMapping(value = "edit", name = "编辑企业信息")
    public String edit(Company company){
        if (StringUtils.isEmpty(company.getId())) {
            company.setId(UUID.randomUUID().toString());
            companyService.save(company);
        } else {
            companyService.update(company);
        }
        return "redirect:/company/list.do";
    }

    @RequestMapping(value = "/toUpdate", name = "跳转编辑企业界面")
    public String toUpdate(String id){
        Company company = companyService.findById(id);
        request.setAttribute("company", company);
        return "company/company-update";
    }

    @RequestMapping(value = "/delete", name = "删除企业信息")
    public String delete(String id){
        companyService.delete(id);
        return "redirect:/company/list.do";
    }
}
